package com.leyou.item.controller;


import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryBrandController {

    @Autowired
    private CategoryBrandService categoryBrandService;

    @Autowired
    private CategoryService categoryService;

//    根据id查询
    @GetMapping("/{id}")
    public ResponseEntity<CategoryDTO> findById(@PathVariable("id") Long id){

        return ResponseEntity.ok(new CategoryDTO(categoryService.getById(id)));
    }
//    查询商品分类集合
    @GetMapping("list")
    public ResponseEntity<List<CategoryDTO>> findCategoryList(@RequestParam("ids") List<Long> ids){

        return ResponseEntity.ok(CategoryDTO.convertEntityList(categoryService.listByIds(ids)));
    }

//    根据父类目id，查询子类目的集合
    @GetMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByParentId(@RequestParam("pid") Long pid){


        return ResponseEntity.ok(CategoryDTO.convertEntityList(categoryService.query().eq("parent_id", pid).list()));
    }

    //    根据品牌id，查询商品分类的集合
    @GetMapping("/of/brand")
    public ResponseEntity<List<CategoryDTO>>queryCategoryByBrandId(@RequestParam("id") Long brandId){

        return ResponseEntity.ok(categoryService.queryCategoryByBrandId(brandId));
    }

}
